<script type="text/javascript">
	var ws, name;

	// 连接服务端
	function connect(port){
		// 创建websocket
		ws = new WebSocket("ws://" + document.domain + ":"+port);
		// 当socket连接打开时，发送登录信息
		ws.onopen = function(){
			var name = "{$userinfo.nickname}";
			// 登录
			var userid = "{$userinfo.id}";
			var login_data = '{"type":"login","client_name":"' + name.replace(/"/g, '\\"') + '","client_id":"'+userid+'"}';
			console.log("websocket握手成功，发送登录数据:" + login_data);
			ws.send(login_data);
		};
		// 当有消息时根据消息类型显示不同信息
		ws.onmessage = onmessage;
		ws.onclose = function(){
			console.log("连接关闭，定时重连");
			connect();
		};
		ws.onerror = function() {
			console.log("出现错误");
		};
	}

	var inte = parseInt(Math.random()*12+1);
	function onmessage(e) {
		var rate = 0;
		var robot_rate = {:C('robot_rate')} ? {:C('robot_rate')} : 3;
		switch({:C('robot_rate')}){
			case 5:
				rate = 5;
				break;
			case 4:
				rate = 10;
				break;
			case 3:
				rate = 25;
				break;
			case 2:
				rate = 35;
				break;
			case 1:
				rate = 45;
				break;
			default:
				rate = 25;
				break;
		}
		// console.log(e.data);
		var data = eval("(" + e.data + ")");
		switch(data['type']) {
			// 服务端ping客户端
			case 'ping':
				$('#xs').html(data.content+{:C('online')});
				ws.send('{"type":"pong"}');
				inte--;
        console.log(inte);
				if(inte==0){
					ws.send('{"type":"robot"}');
                    inte = parseInt(Math.random()*rate+1);
					//inte = parseInt(Math.random()*rate+1)+2;
				}
				break;
				// 登录 更新用户列表
			case 'login':
				console.log(data['client_name'] + "登录成功");
				break;
				// 发言
			case 'say':
				console.log(data['client_name'] + "发言");
				say(data['uid'],data['from_client_name'], data['head_img_url'], data['content'], data['time']);
				break;
				// 用户退出 更新用户列表
			case 'logout':
				break;
			case 'broadcast':
				//alert('client');
			
				//房管
			case 'admin':
				$("#03").prepend('<li class="notice"><center>' + data["content"] + '</center></li>');
				$("#03").prepend('<p class="bettime">'+data["time"]+'</p>');
				document.getElementById("alert1").innerHTML=data["content"];
				$(".alert1").show();
				setTimeout( "$('.alert1').fadeOut()" , 1000);
				break;
				//系统
			case 'system':
				if(data["content"].indexOf("结算已完毕") != -1){
					location.reload();
				}
				$("#03").prepend('<li class="notice"><center>' + data["content"] + '</center></li>');
				$("#03").prepend('<p class="bettime">'+data["time"]+'</p>');
				break;
				//积分减
			case 'points':
				$('#sy').html((parseFloat($('#sy').html())-data['content']).toFixed(1));
				break;
				//积分加
			case 'pointsadd':
				$('#sy').html((parseFloat($('#sy').html())+data['points']).toFixed(1));
				parent.layer.msg('恭喜竞猜成功');
				break;
				//重载
			case 'reload':
				if('{$userinfo.id}'==9){
					window.location.href=window.location.href;
				}
				break;
				//切换
			case 'switch':
				parent.location.reload();
				break;	
		}
	}

	// 提交对话
	function onSubmit(input) {
		var headimgurl = '{$userinfo.headimgurl}';
		var from_client_name = '{$userinfo.nickname}';
		
		if(input==''){
			//$('#textarea').focus();
			return false;
		}
		
		  
		
		ws.send('{"type":"say","client_name":"'+from_client_name+'","headimgurl":"'+headimgurl+'","content":"' + input.replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '"}');
		var num = input.replace(/[^0-9]/ig,"");
				var money = $("#money").text()-num;
				$("#money").text(money.toFixed(2));
		 
		
		//$('#textarea').val('');
		//$('#dialog').scrollTop(0);
	}

	// 发言
	function say(uid, from_client_name, head_img_url, content, time) {
		if('{$userinfo.id}'==uid){
			$("#03").prepend('<li><div><div class="uhead"><img src="'+head_img_url+'" style= "pointer-events: none;"></div><div class="betinfo"><p class="uname">' + from_client_name +'　　'+time +'</p><p class="ubet"><i class="ico02 timeico"></i>理投内容：' + content + '</p></div></div></li>');
		}else{
			$("#03").prepend('<li><div><div class="uhead"><img src="'+head_img_url+'" style= "pointer-events: none;"></div><div class="betinfo"><p class="uname">' + from_client_name +'　　'+time +'</p><p class="ubet"><i class="ico02 timeico"></i>理投内容：' + content + '</p></div></div></li>');
		}
	}
</script>